/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.stakeholder;

import entity.UnstructuredContentEntity;
import entity.UnstructuredContentPageEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.StakeholderUnstructuredContentSessionBean;
import util.entityhelper.UnstructuredContentState;

/**
 *
 * @author WenYong
 */
@ManagedBean
@RequestScoped
public class StakeholderEditUnstructuredContentManagedBean {

    @EJB
    private StakeholderUnstructuredContentSessionBean sessionBean;
    private Long stakeholderId;
    private String nextPage;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private List<UnstructuredContentState> displayList;

    /** Creates a new instance of StakeholderEditUnstructuredContentManagedBean */
    public StakeholderEditUnstructuredContentManagedBean() {
    }

    public void initView(PhaseEvent event) {
        if (displayList == null) {
            displayList = new ArrayList<UnstructuredContentState>();
        }
        stakeholderId = (Long) ctx.getExternalContext().getSessionMap().get("stakeholderId");

        if (stakeholderId == null) {

            nextPage = (ctx.getExternalContext().encodeActionURL(FacesContext.getCurrentInstance().getApplication().getViewHandler().getActionURL(FacesContext.getCurrentInstance(), "/StakeholderLogin.xhtml")));
            try {
                ctx.getExternalContext().redirect(nextPage);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        try {
            List<UnstructuredContentEntity> contentList = sessionBean.getContent(stakeholderId);
            ctx.getExternalContext().getSessionMap().put("content", contentList);
            for(int i=0; i<contentList.size(); i++)
            {
                List<UnstructuredContentPageEntity> pageList = contentList.get(i).getUnstructuredContentPage();
                for (int j = 0; j < pageList.size(); j++) {
                    UnstructuredContentState s = new UnstructuredContentState(pageList.get(j).getPageName(), pageList.get(j).getLang(), pageList.get(j).getTemplateNo());
                    getDisplayList().add(s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void editContent(UnstructuredContentState selectedState) throws IOException {
        String url = "";
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        ctx.getExternalContext().getSessionMap().put("selectedState", selectedState);
        ctx.getExternalContext().getSessionMap().put("language", "remain");
        if (selectedState.getTemplateNo() == 1) {
            url = "StakeholderEditUnstructuredContentTemplateOne.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 2) {
            url = "StakeholderEditUnstructuredContentTemplateTwo.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 3) {
            url = "StakeholderEditUnstructuredContentTemplateThree.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 4) {
            url = "StakeholderEditUnstructuredContentTemplateFour.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 5) {
            url = "StakeholderEditUnstructuredContentTemplateFive.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else {
            url = "StakeholderEditUnstructuredContentTemplateSix.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        }
        nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/" + url));
        ctx.getExternalContext().redirect(nextPage);
    }

     public void editLanguage(UnstructuredContentState selectedState) throws IOException {
        String url = "";
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        ctx.getExternalContext().getSessionMap().put("selectedState", selectedState);
        ctx.getExternalContext().getSessionMap().put("languageE", "edit");
        if (selectedState.getTemplateNo() == 1) {
            url = "StakeholderEditUnstructuredContentLanguageTemplateOne.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 2) {
            url = "StakeholderEditUnstructuredContentLanguageTemplateTwo.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 3) {
            url = "StakeholderEditUnstructuredContentLanguageTemplateThree.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 4) {
            url = "StakeholderEditUnstructuredContentLanguageTemplateFour.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else if (selectedState.getTemplateNo() == 5) {
            url = "StakeholderEditUnstructuredContentLanguageTemplateFive.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        } else {
            url = "StakeholderEditUnstructuredContentLanguageTemplateSix.xhtml";
            ctx.getExternalContext().getSessionMap().put("returnPage", url);
        }
        nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/" + url));
        ctx.getExternalContext().redirect(nextPage);
    }
     
    public List<UnstructuredContentState> getDisplayList() {
        return displayList;
    }

    public void setDisplayList(List<UnstructuredContentState> displayList) {
        this.displayList = displayList;
    }
}